package com.whale.controllers;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.whale.constants.MenuGroupConstants;
import com.whale.constants.RoleConstants;
import com.whale.kernel.domain.UserAccount;

@Controller
@RequestMapping(value = "/users")
public class UsersController extends AbstractController {

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String getLoginForm(HttpServletRequest request, Model model) {
		populateNavigation(model, "/users/login",
				MenuGroupConstants.USER_MENU_GROUP_ID);
		return "users/login";
	}

	@RequestMapping(value = "/createUser", method = RequestMethod.GET)
	public String getCreateUserForm(HttpServletRequest request, Model model) {
		populateNavigation(model, "/users/createUser",
				MenuGroupConstants.USER_MENU_GROUP_ID);
		UserAccount userAccount = new UserAccount();
		model.addAttribute("userAccount", userAccount);
		return "users/createUser";
	}

	@RequestMapping(value = "/createUser", method = RequestMethod.POST)
	public String create(HttpServletRequest request,
			@Valid UserAccount userAccount, BindingResult result, Model model) {
		if (result.hasErrors() || checkExistUsername(userAccount, result)
				|| !checkMatchConfirmPassword(userAccount, result)) {
			populateNavigation(model, "/users/createUser",
					MenuGroupConstants.USER_MENU_GROUP_ID);
			model.addAttribute("userAccount", userAccount);
			return "users/createUser";
		}
		userService.createUser(userAccount,
				Arrays.asList(RoleConstants.ROLE_USER));
		return "redirect:manageUser";
	}

	@RequestMapping(value = "manageUser", method = RequestMethod.GET)
	public String getManageUserForm(HttpServletRequest request, Model model) {
		String currentUser = SecurityContextHolder.getContext().getAuthentication().getName();
		populateNavigation(model, "/users/createUser",
				MenuGroupConstants.USER_MENU_GROUP_ID);
		UserAccount userAccount = userService.findUserByName(currentUser);
		model.addAttribute("userAccount", userAccount);
		return "users/manageUser";
	}

	@RequestMapping(value = "/logoutSuccess", method = RequestMethod.GET)
	public String logoutSuccess(HttpServletRequest request, Model model) {
		return "users/logoutSuccess";
	}

	private boolean checkExistUsername(UserAccount userAccount,
			BindingResult result) {
		UserAccount existUser = userService.findUserByName(userAccount
				.getUsername());
		if (existUser != null) {
			result.rejectValue("username",
					"validation.security.username.alreadyExist");
			return true;
		}
		return false;
	}

	private boolean checkMatchConfirmPassword(UserAccount userAccount,
			BindingResult result) {
		if (!userAccount.getPassword().equals(userAccount.getConfirmPassword())) {
			result.rejectValue("confirmPassword",
					"validation.security.confirmPassword.passwordsNotMatch");
			return false;
		}
		return true;
	}

}
